<template>
  <div class="app-container">
    <el-row>
      <el-col :span="16">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scxx:bomMore:add']"
        >新增</el-button>
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <el-col :span="8">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
                 style="display: flex;justify-content: center">
          <el-form-item prop="bomCode">
            <el-input v-model="queryParams.bomCode" placeholder="选择Bom" readonly
                      @click.native="bomDialogVisible=true;getBomList()" suffix-icon="el-icon-more"></el-input>
          </el-form-item>
          <el-form-item class="yu" style="display: flex">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="bomMoreList"
      row-key="wId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="物料编码" align="center" prop="wlCode" />
      <el-table-column label="物料名称" align="center" prop="wlName" />
      <el-table-column label="BOM编码" align="center" prop="bomCode" />
      <el-table-column label="用量" align="center" prop="many" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scxx:bomMore:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['scxx:bomMore:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scxx:bomMore:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改物料与BOM对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料编码" prop="wlCode">
              <el-input v-model="form.wlCode" placeholder="请输入" readonly
                        @click.native="wlDialogVisible=true;getWlList()" suffix-icon="el-icon-more"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用量" prop="many">
              <el-input v-model="form.many" placeholder="请输入用量" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--    选择bom弹出层-->
    <el-dialog
      title="选择Bom"
      :visible.sync="bomDialogVisible"
      width="30%">
      <el-container>
        <el-header>

          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectBomWithName"
                        v-model="selectBom.bomName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectBomWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectBom.bomName='';getBomList();" type="info">重置</el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-main>

          <!-- 展示物料信息 -->
          <el-table
            :data="bomData"
            height="250"
            border
            style="width: 100%"
            @row-click="getbomData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="bomCode"
              label="Bom编码"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="bomName"
              label="Bom名称"
              align="center"
            >
            </el-table-column>
          </el-table>
          <pagination
            v-show="bomTotal>0"
            :total="bomTotal"
            :page.sync="selectBom.pageNum"
            :limit.sync="selectBom.pageSize"
            @pagination="getBomList"
          />
        </el-main>
      </el-container>
    </el-dialog>

    <!--    选择物料弹出层-->
    <el-dialog
      title="选择物料"
      :visible.sync="wlDialogVisible"
      width="30%">
      <el-container>
        <el-header>

          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectWlWithName"
                        v-model="selectWl.wlName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectWlWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectWl.wlName='';getWlList();" type="info">重置</el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-main>

          <!-- 展示物料信息 -->
          <el-table
            :data="wlData"
            height="250"
            border
            style="width: 100%"
            @row-click="getwlData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="wlCode"
              label="物料编码"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="wlName"
              label="物料名称"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="dwCodename"
              label="单位"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="wlGg"
              label="物料规格"
              align="center"
            >
            </el-table-column>
          </el-table>
          <pagination
            v-show="wlTotal>0"
            :total="wlTotal"
            :page.sync="selectWl.pageNum"
            :limit.sync="selectWl.pageSize"
            @pagination="getWlList"
          />
        </el-main>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
import { listBomMore, getBomMore, delBomMore, addBomMore, updateBomMore } from "@/api/scxx/bomMore";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listWlMess } from "@/api/scxx/wlMess";
import { listBom } from "@/api/scxx/bom";

export default {
  name: "BomMore",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 物料与BOM表格数据
      bomMoreList: [],
      // 物料与BOM树选项
      bomMoreOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        wlCode: null,
        wlName: null,
        wlLevel: null,
        bomCode: null,
        fatherCode: null,
        many: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wlCode: [
          { required: true, message: "物料编码不能为空", trigger: "blur" }
        ],
        wlName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        bomCode: [
          { required: true, message: "BOM编码不能为空", trigger: "blur" }
        ],
        fatherCode: [
          { required: true, message: "父级物料编码不能为空", trigger: "blur" }
        ],
        many: [
          { required: true, message: "用量不能为空", trigger: "blur" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      },
      // 选择物料的弹出层
      wlDialogVisible: false,
      // 物料信息
      wlData:[],
      // 物料搜索值
      selectWl: {
        pageNum: 1,
        pageSize: 10,
        wlName: "",
      },
      // 物料总条数
      wlTotal:0,
      // 选择物料的弹出层
      bomDialogVisible: false,
      // 物料信息
      bomData:[],
      // 物料搜索值
      selectBom: {
        pageNum: 1,
        pageSize: 10,
        bomName: "",
      },
      // 物料总条数
      bomTotal:0,
      // 选中的bomCode
      bomCode:""
    };
  },
  created() {
 // /   this.getList();
  },
  methods: {
    // 根据bom名模糊查询
    selectBomWithName(){
      if (this.selectBom.bomName.trim() != "" ){
        this.getWlList();
      }
    },
    // 获取bom选择的数据
    getbomData(row, column, event){
      this.queryParams.bomCode = row.bomCode
      this.bomCode = row.bomCode
      this.bomDialogVisible = false
      this.handleQuery()
    },
    // 根据条件查询bom
    getBomList(){
      listBom(this.selectBom).then(response => {
        this.bomData = response.rows;
        this.bomTotal = response.total;
      });
    },
    // 根据物料名模糊查询
    selectWlWithName(){
      if (this.selectWl.wlName.trim() != "" ){
        this.getWlList();
      }
    },
    // 获取物料选择的数据
    getwlData(row, column, event){
      this.form.wlCode = row.wlCode
      this.form.wlName = row.wlName
      this.wlDialogVisible = false
    },
    // 根据条件查询物料
    getWlList(){
      listWlMess(this.selectWl).then(response => {
        this.wlData = response.rows;
        this.wlTotal = response.total;
      });
    },
    /** 查询物料与BOM列表 */
    getList() {
      this.loading = true;
      listBomMore(this.queryParams).then(response => {
        this.bomMoreList = this.handleTree(response.data, "wId", "fatherCode");
        this.loading = false;
      });
    },
    /** 转换物料与BOM数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.wId,
        label: node.wlName,
        children: node.children
      };
    },
	/** 查询物料与BOM下拉树结构 */
    getTreeselect() {
      listBomMore().then(response => {
        this.bomMoreOptions = [];
        const data = { wId: 0, wlName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "wId", "fatherCode");
        this.bomMoreOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wId: null,
        wlCode: null,
        wlName: null,
        wlLevel: null,
        bomCode: null,
        fatherCode: null,
        many: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.bomMoreList = []
      // this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.wId) {
        this.form.fatherCode = row.wId;
      } else {
        this.form.fatherCode = 0;
      }
      this.open = true;
      this.title = "添加物料与BOM";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.fatherCode = row.wId;
      }
      getBomMore(row.wId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料与BOM";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.bomCode == '' || this.bomCode == null){
        this.bomDialogVisible = true
        this.getBomList()
        this.$message('请先选择要操作的bom');
        return false
      }
      this.form.bomCode = this.bomCode
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wId != null) {
            updateBomMore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBomMore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除物料与BOM编号为"' + row.wId + '"的数据项？').then(function() {
        return delBomMore(row.wId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
